<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-3"></span><span>
</span><span id="line-4"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- Module      :  Control.Concurrent.Chan</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-9"></span><span class="hs-comment">--</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Portability :  non-portable (concurrency)</span><span>
</span><span id="line-13"></span><span class="hs-comment">--</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Unbounded channels.</span><span>
</span><span id="line-15"></span><span class="hs-comment">--</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- The channels are implemented with @MVar@s and therefore inherit all the</span><span>
</span><span id="line-17"></span><span class="hs-comment">-- caveats that apply to @MVar@s (possibility of races, deadlocks etc). The</span><span>
</span><span id="line-18"></span><span class="hs-comment">-- stm (software transactional memory) library has a more robust implementation</span><span>
</span><span id="line-19"></span><span class="hs-comment">-- of channels called @TChan@s.</span><span>
</span><span id="line-20"></span><span class="hs-comment">--</span><span>
</span><span id="line-21"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-22"></span><span>
</span><span id="line-23"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Control.Concurrent.Chan</span><span>
</span><span id="line-24"></span><span>  </span><span class="hs-special">(</span><span>
</span><span id="line-25"></span><span>          </span><span class="annot"><span class="hs-comment">-- * The 'Chan' type</span></span><span>
</span><span id="line-26"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier">Chan</span></a></span><span class="hs-special">,</span><span>                   </span><span class="hs-comment">-- abstract</span><span>
</span><span id="line-27"></span><span>
</span><span id="line-28"></span><span>          </span><span class="annot"><span class="hs-comment">-- * Operations</span></span><span>
</span><span id="line-29"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#newChan"><span class="hs-identifier">newChan</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#writeChan"><span class="hs-identifier">writeChan</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#readChan"><span class="hs-identifier">readChan</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#dupChan"><span class="hs-identifier">dupChan</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-33"></span><span>
</span><span id="line-34"></span><span>          </span><span class="annot"><span class="hs-comment">-- * Stream interface</span></span><span>
</span><span id="line-35"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#getChanContents"><span class="hs-identifier">getChanContents</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-36"></span><span>        </span><span class="annot"><a href="Control.Concurrent.Chan.html#writeList2Chan"><span class="hs-identifier">writeList2Chan</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-37"></span><span>   </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-38"></span><span>
</span><span id="line-39"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="System.IO.Unsafe.html"><span class="hs-identifier">System.IO.Unsafe</span></a></span><span>         </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.IO.Unsafe.html#unsafeInterleaveIO"><span class="hs-identifier">unsafeInterleaveIO</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-40"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Concurrent.MVar.html"><span class="hs-identifier">Control.Concurrent.MVar</span></a></span><span>
</span><span id="line-41"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Exception.html"><span class="hs-identifier">Control.Exception</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.html#mask_"><span class="hs-identifier">mask_</span></a></span><span class="hs-special">)</span><span class="hs-cpp">

#define _UPK_(x) {-# UNPACK #-} !(x)
</span><span>
</span><span id="line-45"></span><span class="hs-comment">-- A channel is represented by two @MVar@s keeping track of the two ends</span><span>
</span><span id="line-46"></span><span class="hs-comment">-- of the channel contents,i.e.,  the read- and write ends. Empty @MVar@s</span><span>
</span><span id="line-47"></span><span class="hs-comment">-- are used to handle consumers trying to read from an empty channel.</span><span>
</span><span id="line-48"></span><span>
</span><span id="line-49"></span><span class="hs-comment">-- |'Chan' is an abstract type representing an unbounded FIFO channel.</span><span>
</span><span id="line-50"></span><span class="hs-keyword">data</span><span> </span><span id="Chan"><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-var">Chan</span></a></span></span><span> </span><span id="local-6989586621679554532"><span class="annot"><a href="#local-6989586621679554532"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-51"></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="Chan"><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-var">Chan</span></a></span></span><span> </span><span class="hs-identifier">_UPK_</span><span class="hs-special">(</span><span class="hs-identifier">MVar</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Stream</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-52"></span><span>        </span><span class="hs-identifier">_UPK_</span><span class="hs-special">(</span><span class="hs-identifier">MVar</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Stream</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- Invariant: the Stream a is always an empty MVar</span><span>
</span><span id="line-53"></span><span>   </span><span class="hs-keyword">deriving</span><span> </span><span id="local-6989586621679554482"><span id="local-6989586621679554487"><span class="annot"><span class="annottext">Chan a -&gt; Chan a -&gt; Bool
(Chan a -&gt; Chan a -&gt; Bool)
-&gt; (Chan a -&gt; Chan a -&gt; Bool) -&gt; Eq (Chan a)
forall a. Chan a -&gt; Chan a -&gt; Bool
forall a. (a -&gt; a -&gt; Bool) -&gt; (a -&gt; a -&gt; Bool) -&gt; Eq a
/= :: Chan a -&gt; Chan a -&gt; Bool
$c/= :: forall a. Chan a -&gt; Chan a -&gt; Bool
== :: Chan a -&gt; Chan a -&gt; Bool
$c== :: forall a. Chan a -&gt; Chan a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#Eq"><span class="hs-identifier hs-var hs-var hs-var hs-var hs-var hs-var">Eq</span></a></span></span></span><span> </span><span class="hs-comment">-- ^ @since 4.4.0.0</span><span>
</span><span id="line-54"></span><span>
</span><span id="line-55"></span><span class="hs-keyword">type</span><span> </span><span id="Stream"><span class="annot"><a href="Control.Concurrent.Chan.html#Stream"><span class="hs-identifier hs-var">Stream</span></a></span></span><span> </span><span id="local-6989586621679554480"><span class="annot"><a href="#local-6989586621679554480"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#ChItem"><span class="hs-identifier hs-type">ChItem</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554480"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-56"></span><span>
</span><span id="line-57"></span><span class="hs-keyword">data</span><span> </span><span id="ChItem"><span class="annot"><a href="Control.Concurrent.Chan.html#ChItem"><span class="hs-identifier hs-var">ChItem</span></a></span></span><span> </span><span id="local-6989586621679554516"><span class="annot"><a href="#local-6989586621679554516"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="ChItem"><span class="annot"><a href="Control.Concurrent.Chan.html#ChItem"><span class="hs-identifier hs-var">ChItem</span></a></span></span><span> </span><span class="annot"><a href="#local-6989586621679554516"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-identifier">_UPK_</span><span class="hs-special">(</span><span class="hs-identifier">Stream</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">)</span><span>
</span><span id="line-58"></span><span>  </span><span class="hs-comment">-- benchmarks show that unboxing the MVar here is worthwhile, because</span><span>
</span><span id="line-59"></span><span>  </span><span class="hs-comment">-- although it leads to higher allocation, the channel data takes up</span><span>
</span><span id="line-60"></span><span>  </span><span class="hs-comment">-- less space and is therefore quicker to GC.</span><span>
</span><span id="line-61"></span><span>
</span><span id="line-62"></span><span class="hs-comment">-- See the Concurrent Haskell paper for a diagram explaining the</span><span>
</span><span id="line-63"></span><span class="hs-comment">-- how the different channel operations proceed.</span><span>
</span><span id="line-64"></span><span>
</span><span id="line-65"></span><span class="hs-comment">-- @newChan@ sets up the read and write end of a channel by initialising</span><span>
</span><span id="line-66"></span><span class="hs-comment">-- these two @MVar@s with an empty @MVar@.</span><span>
</span><span id="line-67"></span><span>
</span><span id="line-68"></span><span class="hs-comment">-- |Build and returns a new instance of 'Chan'.</span><span>
</span><span id="line-69"></span><span id="local-6989586621679554531"><span class="annot"><a href="Control.Concurrent.Chan.html#newChan"><span class="hs-identifier hs-type">newChan</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554531"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-70"></span><span id="newChan"><span class="annot"><span class="annottext">newChan :: forall a. IO (Chan a)
</span><a href="Control.Concurrent.Chan.html#newChan"><span class="hs-identifier hs-var hs-var">newChan</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-71"></span><span>   </span><span id="local-6989586621679554473"><span class="annot"><span class="annottext">MVar (ChItem a)
</span><a href="#local-6989586621679554473"><span class="hs-identifier hs-var">hole</span></a></span></span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IO (MVar (ChItem a))
forall a. IO (MVar a)
</span><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-var">newEmptyMVar</span></a></span><span>
</span><span id="line-72"></span><span>   </span><span id="local-6989586621679554471"><span class="annot"><span class="annottext">MVar (MVar (ChItem a))
</span><a href="#local-6989586621679554471"><span class="hs-identifier hs-var">readVar</span></a></span></span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar (ChItem a) -&gt; IO (MVar (MVar (ChItem a)))
forall a. a -&gt; IO (MVar a)
</span><a href="GHC.MVar.html#newMVar"><span class="hs-identifier hs-var">newMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (ChItem a)
</span><a href="#local-6989586621679554473"><span class="hs-identifier hs-var">hole</span></a></span><span>
</span><span id="line-73"></span><span>   </span><span id="local-6989586621679554469"><span class="annot"><span class="annottext">MVar (MVar (ChItem a))
</span><a href="#local-6989586621679554469"><span class="hs-identifier hs-var">writeVar</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar (ChItem a) -&gt; IO (MVar (MVar (ChItem a)))
forall a. a -&gt; IO (MVar a)
</span><a href="GHC.MVar.html#newMVar"><span class="hs-identifier hs-var">newMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (ChItem a)
</span><a href="#local-6989586621679554473"><span class="hs-identifier hs-var">hole</span></a></span><span>
</span><span id="line-74"></span><span>   </span><span class="annot"><span class="annottext">Chan a -&gt; IO (Chan a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MVar (MVar (ChItem a)) -&gt; MVar (MVar (ChItem a)) -&gt; Chan a
forall a. MVar (Stream a) -&gt; MVar (Stream a) -&gt; Chan a
</span><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-var">Chan</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (MVar (ChItem a))
</span><a href="#local-6989586621679554471"><span class="hs-identifier hs-var">readVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (MVar (ChItem a))
</span><a href="#local-6989586621679554469"><span class="hs-identifier hs-var">writeVar</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-75"></span><span>
</span><span id="line-76"></span><span class="hs-comment">-- To put an element on a channel, a new hole at the write end is created.</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- What was previously the empty @MVar@ at the back of the channel is then</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- filled in with a new stream element holding the entered value and the</span><span>
</span><span id="line-79"></span><span class="hs-comment">-- new hole.</span><span>
</span><span id="line-80"></span><span>
</span><span id="line-81"></span><span class="hs-comment">-- |Write a value to a 'Chan'.</span><span>
</span><span id="line-82"></span><span id="local-6989586621679554524"><span class="annot"><a href="Control.Concurrent.Chan.html#writeChan"><span class="hs-identifier hs-type">writeChan</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554524"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679554524"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-83"></span><span id="writeChan"><span class="annot"><span class="annottext">writeChan :: forall a. Chan a -&gt; a -&gt; IO ()
</span><a href="Control.Concurrent.Chan.html#writeChan"><span class="hs-identifier hs-var hs-var">writeChan</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679554465"><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554465"><span class="hs-identifier hs-var">writeVar</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679554464"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554464"><span class="hs-identifier hs-var">val</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-84"></span><span>  </span><span id="local-6989586621679554463"><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554463"><span class="hs-identifier hs-var">new_hole</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IO (Stream a)
forall a. IO (MVar a)
</span><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-var">newEmptyMVar</span></a></span><span>
</span><span id="line-85"></span><span>  </span><span class="annot"><span class="annottext">IO () -&gt; IO ()
forall a. IO a -&gt; IO a
</span><a href="GHC.IO.html#mask_"><span class="hs-identifier hs-var">mask_</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; IO () -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-86"></span><span>    </span><span id="local-6989586621679554462"><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554462"><span class="hs-identifier hs-var">old_hole</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar (Stream a) -&gt; IO (Stream a)
forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier hs-var">takeMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554465"><span class="hs-identifier hs-var">writeVar</span></a></span><span>
</span><span id="line-87"></span><span>    </span><span class="annot"><span class="annottext">Stream a -&gt; ChItem a -&gt; IO ()
forall a. MVar a -&gt; a -&gt; IO ()
</span><a href="GHC.MVar.html#putMVar"><span class="hs-identifier hs-var">putMVar</span></a></span><span> </span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554462"><span class="hs-identifier hs-var">old_hole</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a -&gt; Stream a -&gt; ChItem a
forall a. a -&gt; Stream a -&gt; ChItem a
</span><a href="Control.Concurrent.Chan.html#ChItem"><span class="hs-identifier hs-var">ChItem</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554464"><span class="hs-identifier hs-var">val</span></a></span><span> </span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554463"><span class="hs-identifier hs-var">new_hole</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-88"></span><span>    </span><span class="annot"><span class="annottext">MVar (Stream a) -&gt; Stream a -&gt; IO ()
forall a. MVar a -&gt; a -&gt; IO ()
</span><a href="GHC.MVar.html#putMVar"><span class="hs-identifier hs-var">putMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554465"><span class="hs-identifier hs-var">writeVar</span></a></span><span> </span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554463"><span class="hs-identifier hs-var">new_hole</span></a></span><span>
</span><span id="line-89"></span><span>
</span><span id="line-90"></span><span class="hs-comment">-- The reason we don't simply do this:</span><span>
</span><span id="line-91"></span><span class="hs-comment">--</span><span>
</span><span id="line-92"></span><span class="hs-comment">--    modifyMVar_ writeVar $ \old_hole -&gt; do</span><span>
</span><span id="line-93"></span><span class="hs-comment">--      putMVar old_hole (ChItem val new_hole)</span><span>
</span><span id="line-94"></span><span class="hs-comment">--      return new_hole</span><span>
</span><span id="line-95"></span><span class="hs-comment">--</span><span>
</span><span id="line-96"></span><span class="hs-comment">-- is because if an asynchronous exception is received after the 'putMVar'</span><span>
</span><span id="line-97"></span><span class="hs-comment">-- completes and before modifyMVar_ installs the new value, it will set the</span><span>
</span><span id="line-98"></span><span class="hs-comment">-- Chan's write end to a filled hole.</span><span>
</span><span id="line-99"></span><span>
</span><span id="line-100"></span><span class="hs-comment">-- |Read the next value from the 'Chan'. Blocks when the channel is empty. Since</span><span>
</span><span id="line-101"></span><span class="hs-comment">-- the read end of a channel is an 'MVar', this operation inherits fairness</span><span>
</span><span id="line-102"></span><span class="hs-comment">-- guarantees of 'MVar's (e.g. threads blocked in this operation are woken up in</span><span>
</span><span id="line-103"></span><span class="hs-comment">-- FIFO order).</span><span>
</span><span id="line-104"></span><span class="hs-comment">--</span><span>
</span><span id="line-105"></span><span class="hs-comment">-- Throws 'Control.Exception.BlockedIndefinitelyOnMVar' when the channel is</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- empty and no other thread holds a reference to the channel.</span><span>
</span><span id="line-107"></span><span id="local-6989586621679554515"><span class="annot"><a href="Control.Concurrent.Chan.html#readChan"><span class="hs-identifier hs-type">readChan</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554515"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554515"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-108"></span><span id="readChan"><span class="annot"><span class="annottext">readChan :: forall a. Chan a -&gt; IO a
</span><a href="Control.Concurrent.Chan.html#readChan"><span class="hs-identifier hs-var hs-var">readChan</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span id="local-6989586621679554457"><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554457"><span class="hs-identifier hs-var">readVar</span></a></span></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><span class="hs-identifier">_</span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-109"></span><span>  </span><span class="annot"><span class="annottext">MVar (Stream a) -&gt; (Stream a -&gt; IO (Stream a, a)) -&gt; IO a
forall a b. MVar a -&gt; (a -&gt; IO (a, b)) -&gt; IO b
</span><a href="Control.Concurrent.MVar.html#modifyMVar"><span class="hs-identifier hs-var">modifyMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554457"><span class="hs-identifier hs-var">readVar</span></a></span><span> </span><span class="annot"><span class="annottext">((Stream a -&gt; IO (Stream a, a)) -&gt; IO a)
-&gt; (Stream a -&gt; IO (Stream a, a)) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679554455"><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554455"><span class="hs-identifier hs-var">read_end</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-110"></span><span>    </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#ChItem"><span class="hs-identifier hs-type">ChItem</span></a></span><span> </span><span id="local-6989586621679554454"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554454"><span class="hs-identifier hs-var">val</span></a></span></span><span> </span><span id="local-6989586621679554453"><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554453"><span class="hs-identifier hs-var">new_read_end</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Stream a -&gt; IO (ChItem a)
forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#readMVar"><span class="hs-identifier hs-var">readMVar</span></a></span><span> </span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554455"><span class="hs-identifier hs-var">read_end</span></a></span><span>
</span><span id="line-111"></span><span>        </span><span class="hs-comment">-- Use readMVar here, not takeMVar,</span><span>
</span><span id="line-112"></span><span>        </span><span class="hs-comment">-- else dupChan doesn't work</span><span>
</span><span id="line-113"></span><span>    </span><span class="annot"><span class="annottext">(Stream a, a) -&gt; IO (Stream a, a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554453"><span class="hs-identifier hs-var">new_read_end</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554454"><span class="hs-identifier hs-var">val</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-114"></span><span>
</span><span id="line-115"></span><span class="hs-comment">-- |Duplicate a 'Chan': the duplicate channel begins empty, but data written to</span><span>
</span><span id="line-116"></span><span class="hs-comment">-- either channel from then on will be available from both.  Hence this creates</span><span>
</span><span id="line-117"></span><span class="hs-comment">-- a kind of broadcast channel, where data written by anyone is seen by</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- everyone else.</span><span>
</span><span id="line-119"></span><span class="hs-comment">--</span><span>
</span><span id="line-120"></span><span class="hs-comment">-- (Note that a duplicated channel is not equal to its original.</span><span>
</span><span id="line-121"></span><span class="hs-comment">-- So: @fmap (c /=) $ dupChan c@ returns @True@ for all @c@.)</span><span>
</span><span id="line-122"></span><span id="local-6989586621679554511"><span class="annot"><a href="Control.Concurrent.Chan.html#dupChan"><span class="hs-identifier hs-type">dupChan</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554511"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554511"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-123"></span><span id="dupChan"><span class="annot"><span class="annottext">dupChan :: forall a. Chan a -&gt; IO (Chan a)
</span><a href="Control.Concurrent.Chan.html#dupChan"><span class="hs-identifier hs-var hs-var">dupChan</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679554448"><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554448"><span class="hs-identifier hs-var">writeVar</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-124"></span><span>   </span><span id="local-6989586621679554447"><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554447"><span class="hs-identifier hs-var">hole</span></a></span></span><span>       </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar (Stream a) -&gt; IO (Stream a)
forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#readMVar"><span class="hs-identifier hs-var">readMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554448"><span class="hs-identifier hs-var">writeVar</span></a></span><span>
</span><span id="line-125"></span><span>   </span><span id="local-6989586621679554446"><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554446"><span class="hs-identifier hs-var">newReadVar</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Stream a -&gt; IO (MVar (Stream a))
forall a. a -&gt; IO (MVar a)
</span><a href="GHC.MVar.html#newMVar"><span class="hs-identifier hs-var">newMVar</span></a></span><span> </span><span class="annot"><span class="annottext">Stream a
</span><a href="#local-6989586621679554447"><span class="hs-identifier hs-var">hole</span></a></span><span>
</span><span id="line-126"></span><span>   </span><span class="annot"><span class="annottext">Chan a -&gt; IO (Chan a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MVar (Stream a) -&gt; MVar (Stream a) -&gt; Chan a
forall a. MVar (Stream a) -&gt; MVar (Stream a) -&gt; Chan a
</span><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-var">Chan</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554446"><span class="hs-identifier hs-var">newReadVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar (Stream a)
</span><a href="#local-6989586621679554448"><span class="hs-identifier hs-var">writeVar</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-127"></span><span>
</span><span id="line-128"></span><span class="hs-comment">-- Operators for interfacing with functional streams.</span><span>
</span><span id="line-129"></span><span>
</span><span id="line-130"></span><span class="hs-comment">-- |Return a lazy list representing the contents of the supplied</span><span>
</span><span id="line-131"></span><span class="hs-comment">-- 'Chan', much like 'System.IO.hGetContents'.</span><span>
</span><span id="line-132"></span><span id="local-6989586621679554509"><span class="annot"><a href="Control.Concurrent.Chan.html#getChanContents"><span class="hs-identifier hs-type">getChanContents</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554509"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679554509"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span></span><span>
</span><span id="line-133"></span><span id="getChanContents"><span class="annot"><span class="annottext">getChanContents :: forall a. Chan a -&gt; IO [a]
</span><a href="Control.Concurrent.Chan.html#getChanContents"><span class="hs-identifier hs-var hs-var">getChanContents</span></a></span></span><span> </span><span id="local-6989586621679554442"><span class="annot"><span class="annottext">Chan a
</span><a href="#local-6989586621679554442"><span class="hs-identifier hs-var">ch</span></a></span></span><span>
</span><span id="line-134"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO [a] -&gt; IO [a]
forall a. IO a -&gt; IO a
</span><a href="GHC.IO.Unsafe.html#unsafeInterleaveIO"><span class="hs-identifier hs-var">unsafeInterleaveIO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-keyword">do</span><span>
</span><span id="line-135"></span><span>        </span><span id="local-6989586621679554441"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554441"><span class="hs-identifier hs-var">x</span></a></span></span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Chan a -&gt; IO a
forall a. Chan a -&gt; IO a
</span><a href="Control.Concurrent.Chan.html#readChan"><span class="hs-identifier hs-var">readChan</span></a></span><span> </span><span class="annot"><span class="annottext">Chan a
</span><a href="#local-6989586621679554442"><span class="hs-identifier hs-var">ch</span></a></span><span>
</span><span id="line-136"></span><span>        </span><span id="local-6989586621679554440"><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679554440"><span class="hs-identifier hs-var">xs</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Chan a -&gt; IO [a]
forall a. Chan a -&gt; IO [a]
</span><a href="Control.Concurrent.Chan.html#getChanContents"><span class="hs-identifier hs-var">getChanContents</span></a></span><span> </span><span class="annot"><span class="annottext">Chan a
</span><a href="#local-6989586621679554442"><span class="hs-identifier hs-var">ch</span></a></span><span>
</span><span id="line-137"></span><span>        </span><span class="annot"><span class="annottext">[a] -&gt; IO [a]
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554441"><span class="hs-identifier hs-var">x</span></a></span><span class="annot"><span class="annottext">a -&gt; [a] -&gt; [a]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679554440"><span class="hs-identifier hs-var">xs</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-138"></span><span>    </span><span class="hs-special">)</span><span>
</span><span id="line-139"></span><span>
</span><span id="line-140"></span><span class="hs-comment">-- |Write an entire list of items to a 'Chan'.</span><span>
</span><span id="line-141"></span><span id="local-6989586621679554506"><span class="annot"><a href="Control.Concurrent.Chan.html#writeList2Chan"><span class="hs-identifier hs-type">writeList2Chan</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.Chan.html#Chan"><span class="hs-identifier hs-type">Chan</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554506"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679554506"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-142"></span><span id="writeList2Chan"><span class="annot"><span class="annottext">writeList2Chan :: forall a. Chan a -&gt; [a] -&gt; IO ()
</span><a href="Control.Concurrent.Chan.html#writeList2Chan"><span class="hs-identifier hs-var hs-var">writeList2Chan</span></a></span></span><span> </span><span id="local-6989586621679554436"><span class="annot"><span class="annottext">Chan a
</span><a href="#local-6989586621679554436"><span class="hs-identifier hs-var">ch</span></a></span></span><span> </span><span id="local-6989586621679554435"><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679554435"><span class="hs-identifier hs-var">ls</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[IO ()] -&gt; IO ()
forall (t :: * -&gt; *) (m :: * -&gt; *) a.
(Foldable t, Monad m) =&gt;
t (m a) -&gt; m ()
</span><a href="Data.Foldable.html#sequence_"><span class="hs-identifier hs-var">sequence_</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(a -&gt; IO ()) -&gt; [a] -&gt; [IO ()]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Chan a -&gt; a -&gt; IO ()
forall a. Chan a -&gt; a -&gt; IO ()
</span><a href="Control.Concurrent.Chan.html#writeChan"><span class="hs-identifier hs-var">writeChan</span></a></span><span> </span><span class="annot"><span class="annottext">Chan a
</span><a href="#local-6989586621679554436"><span class="hs-identifier hs-var">ch</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679554435"><span class="hs-identifier hs-var">ls</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-143"></span></pre></body></html>